#include "SHA512.h"
#include "TestBase.h"
#include <string>

void RenderArray(unsigned char* arr, int size) // array to print
{
	int width = 8;
	int i, j;
	for (i = 0; i < size; i += width)
	{
		for (j = i; j < (i + width) && j < size; j++)
		{
			if (j > i)
			{
				printf(", ");
			}
			printf("0x%.2x", (int)arr[j]); // make sure printf gets the value as an integer
		}
		if (j < size)
		{
			printf(",\n");
		}
	}
	printf("\n");
}

TestBase::TestBase()
{
	info.Name = "CryptoSystem Test 1 - SHA512";
	info.Category = 0x3100;
	info.CategoryName = "CryptoSystem";
	info.File = __FILE__;
}

bool TestBase::Run()
{
	unsigned char hash1[64] = {
		0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd,
		0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07,
		0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc,
		0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce,
		0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0,
		0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f,
		0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81,
		0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e
	};
	unsigned char output[64];
	int i;
	int fail = 0;
	StateSHA512 state;
	SHA512_Init(&state);
	SHA512_Update(&state, NULL, 0);
	SHA512_Final(&state, output);
	for (i = 0; i < 64; i++)
	{
		if (hash1[i] != output[i])
		{
			fail = 1;
			break;
		}
	}
	if (fail != 0)
	{
		std::cout << GetTestInfo()->Name << " failure info" << std::endl;
		printf("Expected digest:\n");
		RenderArray(hash1, 64);
		printf("Digest:\n");
		RenderArray(output, 64);
		return false;
	}
	return true;
}